﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Net;
using System.Web;
using System.Web.Mvc;
using QLNhaHangTiecCuoi.Models;

namespace QLNhaHangTiecCuoi.Controllers
{
    public class NhaCungCapsController : Controller
    {
        private QLNhaHangTCEntities db = new QLNhaHangTCEntities();

        // GET: NhaCungCaps
        public async Task<ActionResult> Index()
        {
            return View(await db.NhaCungCaps.ToListAsync());
        }

        // GET: NhaCungCaps/Details/5
        public async Task<ActionResult> Details(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            NhaCungCap nhaCungCap = await db.NhaCungCaps.FindAsync(id);
            if (nhaCungCap == null)
            {
                return HttpNotFound();
            }
            return View(nhaCungCap);
        }

        // GET: NhaCungCaps/Create
        public ActionResult Create()
        {
            return View();
        }
        public string CreateMaNCC()
        {
            var item = db.NhaCungCaps.OrderByDescending(i => i.MaNCC).FirstOrDefault();
            string ma;
            if (item == null) { ma = "NCC000"; }
            else ma = item.MaNCC;
            int so = Convert.ToInt32(ma.Substring(3, 3));
            so++;
            string MaNCC = "NCC" + so.ToString("000");
            return MaNCC;
        }
        // POST: NhaCungCaps/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([Bind(Include = "TenNCC,Email,DiaChi,SDT")] NhaCungCap nhaCungCap)
        {
            NhaCungCap NCC = new NhaCungCap()
            {
                MaNCC  =  CreateMaNCC(),
                TenNCC = nhaCungCap.TenNCC,
                Email =  nhaCungCap.Email,
                DiaChi = nhaCungCap.DiaChi,
                SDT = nhaCungCap.SDT
            };

            if (ModelState.IsValid)
            {
                db.NhaCungCaps.Add(NCC);
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            return View(nhaCungCap);
        }

        // GET: NhaCungCaps/Edit/5
        public async Task<ActionResult> Edit(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            NhaCungCap nhaCungCap = await db.NhaCungCaps.FindAsync(id);
            if (nhaCungCap == null)
            {
                return HttpNotFound();
            }
            return View(nhaCungCap);
        }

        // POST: NhaCungCaps/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Edit([Bind(Include = "MaNCC,TenNCC,Email,DiaChi,SDT")] NhaCungCap nhaCungCap)
        {
            if (ModelState.IsValid)
            {
                db.Entry(nhaCungCap).State = EntityState.Modified;
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            return View(nhaCungCap);
        }

        // GET: NhaCungCaps/Delete/5
        public async Task<ActionResult> Delete(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            NhaCungCap nhaCungCap = await db.NhaCungCaps.FindAsync(id);
            if (nhaCungCap == null)
            {
                return HttpNotFound();
            }
            return View(nhaCungCap);
        }

        // POST: NhaCungCaps/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> DeleteConfirmed(string id)
        {
            NhaCungCap nhaCungCap = await db.NhaCungCaps.FindAsync(id);
            db.NhaCungCaps.Remove(nhaCungCap);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
